package com.wyg.epdata.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wyg.epdata.entity.CompanyIntro;
import com.wyg.epdata.entity.CompanyNum;
import com.wyg.epdata.entity.DataResult;
import com.wyg.epdata.entity.reqDto.EntityReq;
import com.wyg.epdata.entity.respones.SourceRes;
import com.wyg.epdata.entity.vo.CountByDate;
import com.wyg.epdata.mapper.CompanyNumMapper;
import com.wyg.epdata.service.ArticleEntityService;
import com.wyg.epdata.service.CompanyDataService;
import com.wyg.epdata.service.CompanyNumService;
import com.wyg.epdata.service.DataResultService;
import com.wyg.epdata.util.DateAndDataUtil;
import com.wyg.epcommonbase.common.ApiResult;
import com.wyg.epcommonbase.common.constant.FinalConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

/**
 * @Author: WangYongGan
 * @Date: 2021/11/8 0:19
 * @Description:
 */
@Api(tags = "企业数据")
@RestController
@RequestMapping("company")
public class CompanyDataController {
    @Autowired
    CompanyNumMapper companyNumMapper;
    @Autowired
    CompanyDataService companyDataService;
    @Autowired
    DataResultService dataResultService;
    @Autowired
    CompanyNumService companyNumService;
    @Autowired
    ArticleEntityService articleEntityService;

    @ApiOperation("获得正负面数量")
    @PostMapping("getCountOfCompany")
    public ApiResult getCountOfCompany(@RequestParam String entity) {
        CompanyNum companyNum = companyNumService.getCountOfCompany(entity);
        return ApiResult.ok().data("reg", companyNum.getRegNum()).data("poi", companyNum.getAllNum() - companyNum.getRegNum());
    }

    @ApiOperation("企业正面数据")
    @PostMapping("getPoiData")
    public ApiResult getPoiData(@RequestBody EntityReq req) {
        final List<String> idsByEntity = articleEntityService.getIdsByEntity(req.getEntity(), 1);
        QueryWrapper<DataResult> wrapper = new QueryWrapper<>();
        final List<DataResult> list = dataResultService.list(wrapper.lambda().in(DataResult::getArticleId, idsByEntity));
        return ApiResult.ok().data(list);
    }

    @ApiOperation("企业模糊查询")
    @PostMapping("like")
    public ApiResult companyLike(@RequestParam String entity) {
        System.out.println(companyDataService);
        return ApiResult.ok().data(companyDataService.like(entity));
    }

    @ApiOperation("热度排名")
    @PostMapping("getHotSort")
    public ApiResult getHotSort(@RequestParam String entity) {
        return ApiResult.ok().data(companyNumService.getHotSort(entity));
    }

    @ApiOperation("企业来源数据")
    @PostMapping("getDataOfPie")
    public ApiResult getDataOfPie(String entity) {

        List<SourceRes> countOfSource = dataResultService.getCountOfSourceByCompany(entity);
        return ApiResult.ok().data(countOfSource);
    }

    @ApiOperation("企业日期数据")
    @PostMapping("getDataByDate")
    public ApiResult getDataByDate(@RequestParam String begin, @RequestParam String end, @RequestParam String entity) {
        List<CountByDate> countRegByDate = companyDataService.getCountRegByDate(begin, end, entity);
        List<CountByDate> countPoiByDate = companyDataService.getCountPoiByDate(begin, end, entity);
        DateAndDataUtil.fillNullBy0Day(begin, end, countPoiByDate);
        DateAndDataUtil.fillNullBy0Day(begin, end, countRegByDate);
        if (FinalConstant.add && countPoiByDate.stream().mapToInt(CountByDate::getSum).sum() > FinalConstant.threshold) {
            Random random = new Random();
            System.out.println("增量：" + random.nextInt(20));
            countPoiByDate.forEach(o -> o.setSum(o.getSum() + random.nextInt(20) + 5));
            countRegByDate.forEach(o -> o.setSum(o.getSum() + random.nextInt(20) + 5));
        }
        return ApiResult.ok().data("totalReg", countRegByDate).data("totalPoi", countPoiByDate);
    }

    @ApiOperation("企业n条负面文本")
    @PostMapping("/getMNRegData")
    public ApiResult getSixRegData(@RequestParam int n, @RequestParam int skip, @RequestParam String entity) {
        String key = n < 0 ? "reg_entity" : "entity";
        Object dataResults = dataResultService.list(new QueryWrapper<DataResult>().like(key, entity).orderByDesc("created_at").last("limit " + skip + "," + n));
        return ApiResult.ok().data(dataResults);
    }

    @ApiOperation("获得企业数量")
    @PostMapping("getCompanyNum")
    public ApiResult getCompanyNum(@RequestParam String entity) {
        CompanyNum companyNum = companyNumService.getCountOfCompany(entity);
        Map<String, Object> map = companyNumService.getMap(new QueryWrapper<CompanyNum>().select("sum(all_num)allNum", "sum(reg_num)regNum"));
        return ApiResult.ok().data("all", companyNum.getAllNum()).data("reg", companyNum.getRegNum());
    }

    @ApiOperation("获取企业简介")
    @PostMapping("getCompanyInfoByName")
    public ApiResult getCompanyInfoByName(@RequestParam String name) {
        QueryWrapper<CompanyIntro> wrapper = new QueryWrapper<CompanyIntro>().eq("name", name);
        CompanyIntro companyIntro = companyDataService.getOne(wrapper);
        String res = companyIntro == null || StringUtils.isEmpty(companyIntro.getIntro()) ? "暂无该企业简介" : companyIntro.getIntro();
        return ApiResult.ok().data(res);
    }

    @ApiOperation("获得企业百科字段")
    @PostMapping("getCompanyBaiKeByName")
    public ApiResult getCompanyBaiKeByName(@RequestParam String name) {
        QueryWrapper<CompanyIntro> wrapper = new QueryWrapper<CompanyIntro>().eq("name", name);
        CompanyIntro companyIntro = companyDataService.getOne(wrapper);
        if (companyIntro == null || StringUtils.isEmpty(companyIntro.getIntro()))
            return ApiResult.ok().data("暂无该企业简介");
        final String baikeKey = companyIntro.getBaikeKey();
        if (StringUtils.isEmpty(baikeKey))
            return ApiResult.ok().data("暂无该企业简介");
        Map<String, String> map = new HashMap<>();
        final String[] keys = baikeKey.split("<-wyg->");
        final String[] values = companyIntro.getBaikeValue().split("<-wyg->");
        for (int i = 0; i < keys.length; i++)
            map.put(keys[i], values[i]);
        return ApiResult.ok().data(map);
    }
}
